﻿/*
*	登录/注册的表单验证
*/
$(document).ready(function () {
    email("#input_reg_username");
    input_password();
    input_verification();
    btn_register();
});
var Is_Email = false;      //email
var Is_Password = false;   //密码
var Is_Verificationcode = false; //验证码
var Is_All = false;    //所有格式正确
var id;       //传入的邮箱的ID 默认为注册的邮箱ID
//email自动提示   
//<input type="text" class="text" name="username" id="input_reg_username" placeholder="常用邮箱" />
function email(str_id) {
    var nowid;
    var totalid;
    var can1press = false;
    var emailafter;
    var emailbefor;
    id = str_id;

    //文本框获得焦点，插入Email提示层
    $(id).focus(function () {
        error_email(1);
        $("#myemail").remove();
        $(this).after("<div id='myemail' style='width:267px; margin-top:22px; height:130px; background:#fff;color:#6B6B6B; text-align:left; position:absolute; left:" + $(this).get(0).offsetLeft + "px; top:" + ($(this).get(0).offsetTop + $(this).height() + 2) + "px; border:1px solid #ccc;z-index:2222; '></div>");
        if ($("#myemail").html()) {
            $("#myemail").css("display", "block");
            $(".newemail").css("width", $("#myemail").width());
            can1press = true;
        }
        else {
            $("#myemail").css("display", "none");
            can1press = false;
        }
    }).blur(function () {//文本框失去焦点时,验证格式是否正确了
        var mail = $(id).val();
        if (mail != '') {
            var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
            if (!reg.test(mail)) {   //错误
                error_email(0);
                Is_Email = false;
            }
            else {
                Is_Email = true;    //完全正确
                error_email(1);
            }
        }
        else {
            Is_Email = false;
            error_email(0);
            $(id).css("color", "#444");

        }
    })
    //文本框输入文字时，显示Email提示层和常用Email
        .keyup(function () {
            var press = $(id).val();
            if (press != "" || press != null) {
                var emailtxt = "";
                var emailvar = new Array("@163.com", "@sohu.com", "@yahoo.com", "@qq.com", "@sina.com", "@gmail.com");
                totalid = emailvar.length;
                //div是自己的输入，第一个选择显示
                var emailmy = "<div class='newemail' style='width:170px; color:#6B6B6B;overflow:hidden;'><font color='#777'>" + press + "</font></div>";
                if (!(isEmail(press))) {
                    for (var i = 0; i < emailvar.length; i++) {
                        //div是所有显示的
                        emailtxt = emailtxt + "<div class='newemail' style='width:170px; color:#6B6B6B;overflow:hidden;'><font color='#777'>" + press + "</font>" + emailvar[i] + "</div>"
                    }
                }
                else {
                    emailbefor = press.split("@")[0];
                    emailafter = "@" + press.split("@")[1];
                    for (var j = 0; j < emailvar.length; j++) {
                        var theemail = emailvar[j];
                        if (theemail.indexOf(emailafter) == 0) {
                            //div是编辑框出现@后的选择
                            emailtxt = emailtxt + "<div class='newemail' style='width:170px; color:#6B6B6B;overflow:hidden;'><font color='#008'>" + emailbefor + "</font>" + emailvar[i] + "</div>"
                        }
                    }
                }
                $("#myemail").html(emailmy + emailtxt);
                if ($("#myemail").html()) {
                    $("#myemail").css("display", "block");
                    $(".newemail").css("width", $("#myemail").width());
                    can1press = true;
                }
                else {
                    $("#myemail").css("display", "none");
                    can1press = false;
                }
                beforepress = press;
            }
            if (press == "" || press == null) {
                $("#myemail").html("");
                $("#myemail").css("display", "none");
            }
        })
    //文本框失焦时删除层
    $(document).click(function () { //文本框失焦时删除层
        if (can1press) {
            $("#myemail").remove();
            can1press = false;
            if ($(id).focus()) {
                can1press = false;
            }
        }
    })
    //鼠标经过提示Email时，高亮该条Email
    $(".newemail").live("mouseover", function () {
        $(".newemail").css("background", "#FFF");
        $(this).css("background", "#CACACA");

        nowid = $(this).index();
    })
    //鼠标点击Email时，文本框内容替换成该条Email，并删除提示层
        .live("click", function () {
            var newhtml = $(this).html();
            newhtml = newhtml.replace(/<.*?>/g, "");
            $(id).val(newhtml);
            $("#myemail").remove();
        })
    };

//检查出现@
    function isEmail(str) {
        if (str.indexOf("@") > 0) {
            return true;
        } else {
            return false;
        }
};
//注册邮箱错误弹出框
function error_email(num) {
    $(id).after("<div class='error_email_div' style='width:270px; margin-top:25px; height:40px;  background:url(../../Content/image/login/login.png); background-position:0px -120px ; position:absolute; left:" + $("#input_reg_username").get(0).offsetLeft + "px; top:" + ($("#input_reg_username").get(0).offsetTop + $("#input_reg_username").height() + 2) + "px; z-index:5px; margin-top:25px; '><label style='margin-left:45px;display:block;text-align:left; height:50px ;width:auto; font:12px 方正姚体;color:#999; padding-top:13px'>请填写正确的邮箱</label></div>");
    if (num == 0) {
        $(".error_email_div").show();
    }
    else if (num == 1) {
        $(".error_email_div").hide();
    }
}
//注册密码错误弹出框
function error_psw(num) {
    $("#input_reg_userpsw").after("<div class='error_psw_div' style='width:270px; height:40px; margin-top:20px; text-align:left; background:url(../../Content/image/login/login.png); background-position:-0px -180px ; position:absolute; left:" + $("#input_reg_userpsw").get(0).offsetLeft + "px; top:" + ($("#input_reg_userpsw").get(0).offsetTop + $("#input_reg_userpsw").height() + 2) + "px; z-index:5px; margin-top:25px; '><label style='margin-left:45px;display:block;text-align:left; height:50px ;width:auto; font:12px 方正姚体; color:#999; padding-top:13px'>请填写正确的密码</label></div>");
    if (num == 0) {
        $(".error_psw_div").show();
    }
    else if (num == 1) {
        $(".error_psw_div").hide();
    }
}
//注册验证码错误弹出框
function error_code(num) {
    $("#input_reg_verification").after("<div class='error_code_div' style='width:100px; margin-top:22px; height:40px;   text-align:left; background:url(../../Content/image/login/login.png); background-position:0px -240px ; position:absolute; left:" + $("#input_reg_verification").get(0).offsetLeft + "px; top:" + ($("#input_reg_verification").get(0).offsetTop + $("#input_reg_verification").height() + 2) + "px; z-index:5px; margin-top:24px; '><label style='margin-left:33px;display:block;text-align:left; height:50px ;width:auto;font:12px 方正姚体; color:#999; padding-top:13px'>验证码错误</label></div>");
    if (num == 0) {
        $(".error_code_div").show();
    }
    else if (num == 1) {
        $(".error_code_div").hide();
    }
}
//密码框验证
function input_password() {
    $("#input_reg_userpsw").focus(function () {

    })
    //密码框失去焦点后
    .blur(function () {
        if ($("#input_reg_userpsw").attr("value") == "") {
            error_psw(0);
        }
        else {
            error_psw(1);
            Is_Password = true;   //完全正确
        }
    })
};
//验证码框验证
function input_verification() {
    var isclick = 0;
    $("#input_reg_verification").focus(function () {
        $("#verification").show();
        $("#input_reg_verification").css("background-position", "-2px -60px");
        $("#input_reg_verification").css("color", "#ccc");
        error_code(1);
    })
    //验证码框失去焦点后
   .blur(function () {
       if (isclick == 0) {   //不是点击刷新按钮
           $("#verification").hide();
           $("#input_reg_verification").css("background-position", "-2px -0px");

           if ($("#input_reg_verification").attr("value") == "") {    //空
               error_code(0);
//               $("#input_reg_verification").css("color", "#444");
           }
           else {
               if (validate("input_reg_verification")) {    //正确
               }
               else {    //错误
                   error_code(0);
//                   $("#input_reg_verification").css("color", "#444");
               }
           }
       }
       else {    //当前点击了刷新按钮
//           $("#input_reg_verification").css("color", "#444");
           $("#input_reg_verification").focus();
       }
   });
    //判断验证框失去焦点是是否是因为刷新验证码
    $(function () {
        $("#verification").hover(function () {
            isclick = 1;    //是   当前点击了刷新按钮  则不隐藏div
        },
         function () {
             isclick = 0;
         });
    });
};
//注册按钮
function btn_register() {
    $("#btn_register").click(function (event) {
        var str_email = $("#input_reg_username").attr("value");
        if (str_email == "") {    //邮箱
            Is_Email = false;
            $("#input_reg_username").focus();
            error_email(0); Is_All = false; return;
        }
        var str_psw = $("#input_reg_userpsw").attr("value");
        if (str_psw == "") {      //密码
            Is_Password = false;
            $("#input_reg_userpsw").focus();
            error_psw(0); Is_All = false; return;
        }
        var str_code = $("#input_reg_verification").attr("value");
        if (str_code == "") {     //验证码
            Is_Verificationcode = false;
            error_code(0);
            Is_All = false;
        }
        else {
            Is_All = true;
        }
    });
}
